//
//  WDCancelWorkOrderViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/6.
//

#import "WDCancelWorkOrderViewController.h"
#import "WDCancelWorkOrderViewModel.h"
#import "WDCancelWorkOrderTopView.h"
#import "WDCancelWorkOrderBottomView.h"
#import "TYZPlaceholderTextView.h"

@interface WDCancelWorkOrderViewController () <UITextViewDelegate>
@property (nonatomic, strong) WDCancelWorkOrderViewModel *viewModel;

@property (nonatomic, strong) WDCancelWorkOrderTopView *topView;

@property (nonatomic, strong) WDCancelWorkOrderBottomView *bottomView;

/// 处理说明标题
@property (nonatomic, strong) UILabel *remarkLabel;

/// 处理说明输入框
@property (nonatomic, strong) TYZPlaceholderTextView *remarkView;


/// 处理说明标题
- (void)initWithRemarkLabel;

/// 处理说明输入框
- (void)initWithRemarkView;

@end

@implementation WDCancelWorkOrderViewController

- (void)dealloc {
    debugMethod();
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [WDCancelWorkOrderViewModel new];
    _viewModel.serviceModel = self.serviceModel;
    [_viewModel setupData];
}

- (void)initWithNavBar {
    [super initWithNavBar];
    
    [self initWithBackButton];
    
    self.navigationItem.title = self.viewModel.title;
    
}

- (void)initWithSubView {
    [super initWithSubView];
    
    [self initWithRootContent:NO];
    self.rootContent.height = self.rootContent.height - TABBAR_HEIGHT;
    
    [self initWithTopView];
    
    [self initWithBottomView];
    
    
    /// 处理说明标题
    [self initWithRemarkLabel];

    /// 处理说明输入框
    [self initWithRemarkView];

}



- (void)initWithTopView {
    if (!_topView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kWDCancelWorkOrderTopViewHeight);
        _topView = [[WDCancelWorkOrderTopView alloc] initWithFrame:frame];
        
        [self.rootContent addSubview:_topView];
    }
    [_topView updateWithViewData:self.viewModel.serviceModel];
}

- (void)initWithBottomView {
//    if (!_bottomView) {
        CGRect frame = CGRectMake(0, self.rootContent.bottom, kScreenW, TABBAR_HEIGHT);
        _bottomView = [[WDCancelWorkOrderBottomView alloc] initWithFrame:frame];
        [self.view addSubview:_bottomView];
        
        @weakify(self);
    _bottomView.viewBlock = ^(id  _Nullable data) {
        @strongify(self);
        [self actionWithSubmitCancel];
    };
}

/// 处理说明标题
- (void)initWithRemarkLabel {
    if (!_remarkLabel) {
        CGRect frame = CGRectMake(20, self.topView.bottom + 12, kScreenW - 40, 20);
        _remarkLabel = [UILabel createWithLabel:self.rootContent frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
//            label.text = @"处理说明";
        }];
        _remarkLabel.attributedText = [EDSUtility attributeTitle:@"* " titleColor:[UIColor colorWithHexString:@"#F24C55"] titleFont:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] value:@"处理说明" valueColor:[UIColor colorWithHexString:@"#000000"] valueFont:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular]];
    }
}

/// 处理说明输入框
- (void)initWithRemarkView {
    if (!_remarkView) {
        CGRect frame = CGRectMake(20, self.remarkLabel.bottom + 8, self.remarkLabel.width, 130);
        _remarkView = [[TYZPlaceholderTextView alloc] initWithFrame:frame];
        _remarkView.font = FONT_Regular(14);
        _remarkView.delegate = self;
        _remarkView.clipsToBounds = YES;
        _remarkView.textColor = UIColorFromHex(0x000000, 1.0);
        _remarkView.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
        _remarkView.textContainer.lineFragmentPadding = 0;
        _remarkView.textContainerInset = UIEdgeInsetsMake(15, 9, 15, 9);
        _remarkView.placeholder = @"请输入取消工单原因，不少于20汉字；";
        _remarkView.placeholderFont = FONT_Regular(14);
        _remarkView.placeholderColor = [UIColor colorWithHexString:@"#BBBBBB"];
        _remarkView.layer.borderColor = [UIColor colorWithHexString:@"#DDDDDD"].CGColor;
        _remarkView.layer.borderWidth = 1;
        _remarkView.layer.cornerRadius = 8;
        _remarkView.layer.masksToBounds = YES;
        [self.rootContent addSubview:_remarkView];
        
        RAC(self.viewModel.reqModel, applyReason) = self.remarkView.rac_textSignal;
    }
}

/// 确认取消
- (void)actionWithSubmitCancel {
    NSString *applyReason = objectNull(self.viewModel.reqModel.applyReason);
    if ([applyReason isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"请输入处理说明！"];
        return;
    }
    if (applyReason.length > 200) {
        [SVProgressHUD showInfoWithStatus:@"请输入处理说明不能超过200个字！"];
        return;
    }
    
    
    
    [SVProgressHUD showWithStatus:@"取消申请中..."];
    [self.viewModel requestWithCancelApply:^(EDSRespondDataEntity * _Nonnull respond) {
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [SVProgressHUD showSuccessWithStatus:@"申请取消提交成功！"];
            if (self.popResultBlock) {
                self.popResultBlock(nil);
            }
            [self clickedBack:nil];
        } else {
            if (![objectNull(respond.msg) isEqualToString:@""]) {
                [SVProgressHUD showInfoWithStatus:respond.msg];
            }
        }
    }];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
//    CGSize sizeToFit = [textView sizeThatFits:CGSizeMake(kRealValue(343), CGFLOAT_MAX)];
//    CGFloat heigth = sizeToFit.height;
//    [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.detailAddressLabel.mas_bottom).offset(kRealValue(14.5));
//        make.height.mas_equalTo(heigth);
//        make.width.mas_equalTo(kRealValue(343));
//        make.left.equalTo(self.detailAddressLabel.mas_left);
//        make.bottom.equalTo(self.detailAddressView.mas_bottom).offset(-kRealValue(12));
//    }];
}



@end
